package com.ken.elmboot.controller;

import com.ken.elmboot.bean.Business;
import com.ken.elmboot.common.ElmException;
import com.ken.elmboot.common.Pager;
import com.ken.elmboot.common.Result;
import com.ken.elmboot.service.BusinessService;
import com.ken.elmboot.util.PasswordUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ken
 */
@RestController
@RequestMapping("/sys/business")
public class BusinessController {
    private Logger logger = LoggerFactory.getLogger(BusinessController.class);

    @Autowired
    private BusinessService businessService;

    @RequestMapping("add")
    public Result add(Business business) {
        Result result = new Result();
        String salt = PasswordUtils.getSalt();
        String password = PasswordUtils.encode(business.getPassword(),salt);
        business.setSalt(salt);
        business.setPassword(password);
        try {
            boolean ret = businessService.add(business);
            if (ret) {
                result.setSuccess(true);
            } else {
                result.setMsg("新增商户失败");
            }
        }catch (ElmException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping("del")
    public Result delete(Integer businessId) {
        Result result = new Result();
        if(businessId != null) {
            try {
                boolean ret = businessService.deleteById(businessId);
                if(ret) {
                    result.setSuccess(Boolean.TRUE);
                    result.setMsg("删除成功");
                }else {
                    result.setMsg("删除失败");
                }
            }catch (ElmException e) {
                result.setMsg(e.getMessage());
            }
        }else {
            result.setMsg("用户不存在");
        }
        return result;
    }

    @RequestMapping("edit")
    public Result edit(Business business) {
        Result result = new Result();
        try {
            String salt = PasswordUtils.getSalt();
            String password = PasswordUtils.encode(business.getPassword(),salt);
            business.setSalt(salt);
            business.setPassword(password);
            boolean ret = businessService.edit(business);
            if(ret) {
                result.setSuccess(Boolean.TRUE);
                result.setMsg("修改商户信息成功");
            }else {
                result.setMsg("修改商户信息失败");
            }
        }catch (ElmException e) {
            result.setMsg("修改商户信息异常");
        }
        return result;
    }

    @RequestMapping("search")
    public Pager<Business> list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                @RequestParam(value = "size",defaultValue = "10") Integer size) {
        Pager<Business> pager = new Pager<>(page,size);
        logger.info(pager.toString());
        int total = businessService.findTotal(pager);
        List<Business> list = businessService.findByPager(pager);
        pager.setTotal(total);
        pager.setRows(list);
        logger.info(pager.toString());
        return pager;
    }

}
